#include "pluginsmanagedialog.h"
#include "ui_pluginsmanagedialog.h"

PluginsManageDialog::PluginsManageDialog(QWidget *parent,ROLE role) :
    QDialog(parent),
    m_ui(new Ui::PluginsManageDialog)
{
    m_ui->setupUi(this);
    switch(role)
    {
        case MANAGE:
        {
            m_ui->addPluginButtonBox->setShown(false);
            m_ui->resultButtonBox->setShown(true);
        }
        break;

        case ADD:
        {
            m_ui->addPluginButtonBox->setShown(true);
            m_ui->resultButtonBox->setShown(false);
        }
        break;
        default:
        {
            m_ui->addPluginButtonBox->setShown(true);
            m_ui->resultButtonBox->setShown(true);
        }
    }
    m_activePlugin = 0;
    connect(m_ui->addPluginButtonBox,
            SIGNAL(accepted()),
            this,
            SLOT(on_addPluginButtonBox_accepted()));
    showPlugins();
}

void PluginsManageDialog::showPlugins()
{
    QVectorIterator<Plugin*> iteration(*plugins);
    while(iteration.hasNext())
    {
        Plugin* plug= iteration.next();
        PluginManageTreeWidgetItem* item = new PluginManageTreeWidgetItem(plug->name());
        item->setPlugin(plug);
        m_ui->pluginsListWidget->addItem(item);
    }
}

PluginsManageDialog::~PluginsManageDialog()
{
    delete m_ui;
    //if(m_activePlugin != 0)    //delete m_activePlugin;
}

void PluginsManageDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void  PluginsManageDialog::on_addPluginButtonBox_accepted()
{
    m_activePlugin = (static_cast<PluginManageTreeWidgetItem*>(m_ui->pluginsListWidget->currentItem()))->getPlugin();
    close();
}

Plugin* PluginsManageDialog::getPlugin()
{
    return m_activePlugin;
}

void PluginsManageDialog::on_pluginsListWidget_itemPressed(QListWidgetItem* item)
{
    Q_UNUSED(item);
    m_ui->addPluginButtonBox->setEnabled(true);
}

void PluginsManageDialog::on_resultButtonBox_rejected()
{

}

void PluginsManageDialog::on_addPluginButtonBox_rejected()
{
    delete m_activePlugin;
    m_activePlugin = 0;
}

void PluginsManageDialog::on_PluginsManageDialog_rejected()
{
    delete m_activePlugin;
    m_activePlugin = 0;
}
